package org.basket.core;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/*
 * BasketMessage 
 * [kind:"BasketMessage", id:"YYYY-MM-DD-email"]
 *      [date: "YYYY-MM-DD" user: "mail" presence: Boolean, ball: Boolean, message: "the message"]
 */
public class BasketMessage implements EntityAtom {
	public static final String KIND = "BasketMessage";

	public static final String DATE = "date";
	public static final String EMAIL = "email";
	public static final String PRESENCE = "presence";
	public static final String BALL = "ball";
	public static final String MESSAGE = "message";
	public static final String USERNAME = "username";

	private final String date;// date YYY-MM-DD
	private final String email;
	public final Boolean presence;
	public final Boolean ball;
	public final String message;
	public final String username;

	public BasketMessage(String date, String email, Boolean presence, Boolean ball, String message, String username) {
		this.date = date;
		this.email = email.toLowerCase();
		this.presence = (presence == null) ? Boolean.FALSE : presence;
		this.ball = this.presence && ((ball == null) ? Boolean.FALSE : ball);
		this.message = message;
		this.username = username;
	}

	public Entity getEntity() {
		Key key = KeyFactory.createKey(KeyFactory.createKey(date, date), KIND, date + "-" + email);
		Entity entity = new Entity(key);
		entity.setProperty(DATE, date);
		entity.setProperty(EMAIL, email);
		entity.setProperty(PRESENCE, presence);
		entity.setProperty(BALL, ball);
		entity.setProperty(MESSAGE, message);
		entity.setProperty(USERNAME, username);
		return entity;
	}
}
